package com.yy.mydota;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yy.bean.Hero;
import com.yy.database.DBHelperService;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 物品界面展示
 * @author liyuye
 * @date 2015-5-20 下午3:17:26
 */
public class ArticlesFragment extends Fragment {
	private static String TAG = "ResourceFragment";
//	private GridView gridview_hero;
	private List<Hero> heroNames;
	
	private LinearLayout ll_fenlei;

//	private MyAdapter myAdapter;
	private DBHelperService ds;
	
	private static String GROUP_SQL = "select H_TYPE from HEROS GROUP BY H_TYPE";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fra_heros, container, false);
//		gridview_hero = (GridView) view.findViewById(R.id.gridview_hero);
		ll_fenlei = (LinearLayout) view.findViewById(R.id.ll_fenlei);
		ll_fenlei.setVisibility(View.GONE);
		getAllName();

//		myAdapter = new MyAdapter(getActivity());
//		gridview_hero.setAdapter(myAdapter);
		return view;
	}

	private List<Hero> getAllName() {
		heroNames = new ArrayList<Hero>();
		ds = new DBHelperService(getActivity());
		Hero hero;
		Cursor cursor = ds.getAllRecords("HEROS");
		for(int i=0;i<cursor.getCount();i++){
			hero = new Hero();
			cursor.moveToPosition(i);
			hero.setHeroID(cursor.getString(cursor.getColumnIndex("HERO_ID")));
			hero.setName(cursor.getString(cursor.getColumnIndex("HERO_NAME")));
			hero.setImageID(cursor.getInt(cursor.getColumnIndex("H_IMAGE")));
			heroNames.add(hero);
		}
		cursor.close();
		ds.closeDB();
		return heroNames;
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	class MyAdapter extends BaseAdapter {
		private Context mContext;

		class ListItemView {
			public ImageView iv_hero_ico;
			public TextView texv_hero_name;
		}

		public MyAdapter(Context context) {
			mContext = context;
		}

		@Override
		public int getCount() {
			return heroNames == null ? 0 : heroNames.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ListItemView listItem = new ListItemView();
			if (convertView == null) {
				convertView = View.inflate(mContext, R.layout.item_hero_list,
						null);
				listItem.iv_hero_ico = (ImageView) convertView
						.findViewById(R.id.iv_hero_ico);
				listItem.texv_hero_name = (TextView) convertView
						.findViewById(R.id.texv_hero_name);
				convertView.setTag(listItem);
			} else {
				listItem = (ListItemView) convertView.getTag();
			}

			listItem.texv_hero_name.setText(heroNames.get(position).getName());
			listItem.iv_hero_ico.setImageResource(heroNames.get(position).getImageID());
			return convertView;
		}

	}
}
